using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NTTHAO.Common;
using NTTHAO.CommonWeb;
using NTTHAO.Web.BL.Panel;

public partial class admin_webparts_Plug_ins_Panel_PanelGroupInput : System.Web.UI.UserControl
{
    public string _ErrorMessage = String.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnSave2.Text = this.btnSave.Text = LanguageUtility.GetLanguageText("SAVE", Session);
        this.chkActive.Text = LanguageUtility.GetLanguageText("ACTIVE", Session);
        this.txtDefaultWidth.Style[HtmlTextWriterStyle.TextAlign] = "right";
        this.txtDefaultHeight.Style[HtmlTextWriterStyle.TextAlign] = "right";
        this.txtMaxWidth.Style[HtmlTextWriterStyle.TextAlign] = "right";
        this.txtMaxHeight.Style[HtmlTextWriterStyle.TextAlign] = "right";

        
        if (!IsPostBack)
        {
            if (Request.QueryString["id"] != null)
            {
                try
                {
                    if (!"groupedit".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase))
                    {
                        throw new Exception();
                    }

                    PanelGroup group = new PanelGroup(Int32.Parse(Request.QueryString["id"]));

                    if (group.ID < 1)
                    {
                        throw new Exception();
                    }

                    this.txtName.Text = group.Name;
                    this.txtDefaultWidth.Text = group.DefaultWidth.ToString();
                    this.txtDefaultHeight.Text = group.DefaultHeight.ToString();
                    this.txtMaxWidth.Text = group.MaxWidth.ToString();
                    this.txtMaxHeight.Text = group.MaxHeight.ToString();
                    this.chkActive.Checked = group.Actived;
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message + "<br /><br />" + ex.StackTrace);
                    //Response.Redirect("?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"], false);
                    return;
                }
            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            PanelGroup group = null;

            if ("groupedit".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase))
            {
                group = new PanelGroup(Int32.Parse(Request.QueryString["id"].Trim()));
            }
            else
            {
                group = new PanelGroup();
                group.SiteID = WebGlobal.GetSite(Session).ID;
                group.LanguageID = LanguageUtility.GetLanguageID(Request, Session);
            }

            group.Name = txtName.Text;
            group.DefaultWidth = Int32.Parse(txtDefaultWidth.Text.Trim());
            group.DefaultHeight = Int32.Parse(txtDefaultHeight.Text.Trim());
            group.MaxWidth = Int32.Parse(txtMaxWidth.Text.Trim());
            group.MaxHeight = Int32.Parse(txtMaxHeight.Text.Trim());
            group.Actived = chkActive.Checked;

            if ("groupedit".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase))
            {
                group.Update(); 
            }
            else
            {
                group.ImagePath = Constant.UPLOAD_FILE_PATH + "panel/"; //"resources/uploaded/panel/";
                group.Insert();
                group.ImagePath = Constant.UPLOAD_FILE_PATH + "panel/" + group.ID.ToString() + "/";
                group.Update();
            }
            Response.Redirect("?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"], false);
        }
        catch (Exception ex)
        {
            this._ErrorMessage = ex.Message;
        }
    }
}
